package com.easychat.mychat.common.rabbitMq;


import com.easychat.mychat.common.entity.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


/**
 * @Description rabbitmq配置
 * @Author susu
 * @Date 2024/9/26
 */
@Configuration
@Slf4j
public class RabbitMqConfig {

    @Value("${mychat.queue.to:}")
    private String queueName;


    @Bean
    public Queue chatQueue(){
        return new Queue(queueName,true);
    }
    @Bean
    public TopicExchange chatExchange(){
        return new TopicExchange(Constants.EXCHANGE_CHAT_TOPIC_NAME);
    }
    @Bean
    public Binding chatBinding(Queue chatQueue, TopicExchange chatExchange){
        return BindingBuilder.bind(chatQueue).to(chatExchange).with(Constants.ROUTING_KEY_CHAT);
    }

    @Bean
    public Queue delayQueue(){return new Queue(Constants.QUEUE_CHAT_DELAYED,true);}
    @Bean
    public CustomExchange delayExchange(){
        HashMap<String,Object> args = new HashMap<>();
        //自定义交换机的类型
        args.put("x-delay-type","direct");
        /*
        * 交换机名
        * 交换机类型
        * 持久化
        * 自动删除
        * */
        return new CustomExchange(Constants.EXCHANGE_CHAT_DELAYED,"x-delayed-message",true,false,args);
    }
    @Bean
    public Binding delayBinding(Queue delayQueue, CustomExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(Constants.ROUTING_KEY_DELAYED).noargs();
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }

    @Bean
    public SimpleMessageListenerContainer delayedContainer(ConnectionFactory connectionFactory){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }


    //测试用，对性能影响大
//    @PostConstruct
//    public void init(){
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage returnedMessage) {
//                log.error("监听到了消息return callback");
//                log.debug("exchange：{}",returnedMessage.getExchange());
//                log.debug("routingkey：{}",returnedMessage.getRoutingKey());
//                log.debug("message：{}",returnedMessage.getMessage());
//                log.debug("replyCode：{}",returnedMessage.getReplyCode());
//                log.debug("replyText：{}",returnedMessage.getReplyText());
//            }
//        });
//    }

}
